package com.sfx.算法专题.链表.合并链表;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-07-30
 * Time: 15:03
 */
public class Code_3_mergeInBetween {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    static class Solution {
        public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
            ListNode cur = list1;
            ListNode next = null;
            ListNode tmp = list2;
            int len2 = 0;
            while(tmp!=null) {
                tmp = tmp.next;
                len2 ++;
            }
            int gap = b-a +2;
            while(a>1) {
                cur = cur.next;
                a--;
            }
            tmp = cur;
            while(gap > 0) {
                tmp = tmp.next;
                gap --;
            }
            next = tmp;
            cur.next = list2;
            while(len2>0) {
                cur = cur.next;
                len2--;
            }
            cur.next = next;

            return list1;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();

    }
}
